package com.itheima.tanhua.apiImpl;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.tanhua.api.mongodb.CommentApi;
import com.itheima.tanhua.enums.CommentType;
import com.itheima.tanhua.pojo.mongo.Comment;
import com.itheima.tanhua.pojo.mongo.Movement;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Slf4j
@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 通过movementId 查询评论
     * @param page
     * @param pagesize
     * @param movementId
     * @param comment
     * @return
     */
    @Override
    public List<Comment> findComments(Integer page, Integer pagesize, String movementId, CommentType comment) {

        //构造查询的条件
        log.info("{}",movementId);
        Query query=Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(comment.getType()))
                .skip((page-1)*pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        log.info("11111");
        //查询mongodb 并返回值
        return mongoTemplate.find(query, Comment.class);

    }

    //保存评论  并获取总数
    @Override
    public Integer saveComment(Comment comment) {
        //补全信息 publishUserId
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (ObjectUtil.isNotEmpty(movement)){
            comment.setPublishUserId(movement.getUserId());
        }
        //存储Comment到数据库
        mongoTemplate.save(comment);
        //4、更新动态表中的对应字段

        //第一个值 query  更新动态表中的对应字段
        Query query=Query.query(Criteria.where("id").is(comment.getPublishId()));
        //第二个更新的对象
        Update update=new Update();
        if (comment.getCommentType()==CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if (comment.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //第三个是设置更新的参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        //第四个数据返回值类型
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //需要的值都存储到  modity中
        //返回最新的评论数量 并返回
        return modify.statisCount(comment.getCommentType());
    }

    //判断此集合是否存在,存在就已经被点赞过了
    @Override
    public boolean isComment(String movementId, Long uid, CommentType commentType) {

        Criteria criteria=Criteria.where("publishId").is(movementId)
                .and("userId").is(uid).and("commentType").is(commentType);
        Query query=Query.query(criteria);

         return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer deleteComment(Comment comment){
        //补全信息 publishUserId
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (ObjectUtil.isNotEmpty(movement)){
            comment.setPublishUserId(movement.getUserId());
        }
        //存储Comment到数据库
        mongoTemplate.save(comment);
        //4、更新动态表中的对应字段

        //第一个值 query  更新动态表中的对应字段
        Query query=Query.query(Criteria.where("id").is(comment.getPublishId()));
        //第二个更新的对象
        Update update=new Update();
        if (comment.getCommentType()==CommentType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if (comment.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //第三个是设置更新的参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        //第四个数据返回值类型
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        //需要的值都存储到  modity中
        //返回最新的评论数量 并返回
        return modify.statisCount(comment.getCommentType());
    }

}
